package cn.ncist.health_service_provider.service.impl;

import cn.ncist.dto.ReportData;
import cn.ncist.dto.SetmealDto;
import cn.ncist.health_interface.service.ReportService;
import cn.ncist.health_service_provider.dao.OrderMapper;
import cn.ncist.health_service_provider.dao.ReportMapper;
import cn.ncist.health_service_provider.dao.SetMealMapper;
import cn.ncist.pojo.Member;
import cn.ncist.pojo.Order;
import cn.ncist.pojo.Setmeal;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.*;

@Slf4j
@Service
@DubboService(interfaceClass = ReportService.class, timeout = 50000)
public class ReportServiceImpl implements ReportService {

    private ReportData reportData = new ReportData();


    @Autowired
    private ReportMapper reportMapper;

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private SetMealMapper setMealMapper;

    @Override
    public synchronized ReportData exportBusinessReport(Date date) {
        findMemberInfo(date);
        findOrderInfo(date);
        List<Map<String, Object>> setmealCount = setMealMapper.findSetmealCount();
        Integer sum = 0;
        List<SetmealDto> setmealDtoList = new ArrayList<>();
        DecimalFormat df = new DecimalFormat("#.00");
        for (Map<String, Object> map : setmealCount) {
            Integer count = Integer.parseInt(String.valueOf((Long)map.get("value")));
            sum = sum + count;
        }
        for (Map<String, Object> map : setmealCount) {
            SetmealDto setmealDto = new SetmealDto();
            setmealDto.setName((String) map.get("name"));
            setmealDto.setValue(String.valueOf(map.get("value")));
            double d = Integer.parseInt(String.valueOf(map.get("value"))) / sum;
            setmealDto.setProportion(Double.valueOf(df.format(new BigDecimal(d))));
            setmealDtoList.add(setmealDto);
        }
        reportData.setHotSetmeal(setmealDtoList);
        return reportData;

    }

    /**
     * 查询会员信息
     *
     * @param date
     */
    public void findMemberInfo(Date date) {
        int totalMember = 0;//总的会员数
        int todayNewMember = 0;//今日新增会员数
        int thisWeekNewMember = 0;//这周新增会员数
        int thisMonthNewMember = 0;//这个月新增会员数

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

//        String lastWeekFromNow = sdf.format(new Date(System.currentTimeMillis() - 7 * 24 * 60 * 60 * 1000));
//        String lastMonthFromNow = sdf.format(new Date(System.currentTimeMillis() - 30L * 24 * 60 * 60 * 1000));

        String dateStr = sdf.format(date);
        List<Member> memberList = reportMapper.findTotalMember();
        totalMember = memberList.size();//总的会员数
        for (Member member : memberList) {
            if (dateStr.equals(sdf.format(member.getRegTime()))) {
                ++todayNewMember;//今日新增会员数
            }

            if (member.getRegTime().getTime() > date.getTime() - 7 * 24 * 60 * 60 * 1000) {
                ++thisWeekNewMember;//这周新增会员数
            }

            if (member.getRegTime().getTime() > date.getTime() - 30L * 24 * 60 * 60 * 1000) {
                ++thisMonthNewMember;//这个月新增会员数
            }
        }
        reportData.setTotalMember(totalMember);
        reportData.setTodayNewMember(todayNewMember);
        reportData.setThisWeekNewMember(thisWeekNewMember);
        reportData.setThisMonthNewMember(thisMonthNewMember);
    }

    /**
     * 查询预约信息
     *
     * @param date
     */
    public void findOrderInfo(Date date) {
        int todayOrderNumber = 0;//本日预约数
        int thisWeekOrderNumber = 0;//本周预约数
        int thisMonthOrderNumber = 0;//本月预约数

        int todayOrderVisitNumber = 0;//本日到诊数
        int thisWeekOrderVisitNumber = 0;//本周到诊数
        int thisMonthOrderVisitNumber = 0;//本月到诊数

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        List<Order> orderList = orderMapper.selectList(new LambdaQueryWrapper<>());
        for (Order order : orderList) {
            if (sdf.format(date).equals(sdf.format(order.getOrderDate()))) {
                ++todayOrderNumber;
                if ("已到诊".equals(order.getOrderStatus())) {
                    ++todayOrderVisitNumber;
                }
            }

            if (order.getOrderDate().getTime() > date.getTime() - 7 * 24 * 60 * 60 * 1000) {
                ++thisWeekOrderNumber;
                if ("已到诊".equals(order.getOrderStatus())) {
                    ++thisWeekOrderVisitNumber;
                }
            }

            if (order.getOrderDate().getTime() > date.getTime() - 30L * 24 * 60 * 60 * 1000) {
                ++thisMonthOrderNumber;
                if ("已到诊".equals(order.getOrderStatus())) {
                    ++thisMonthOrderVisitNumber;
                }
            }
        }
        reportData.setTodayOrderNumber(todayOrderNumber);
        reportData.setTodayVisitsNumber(todayOrderVisitNumber);
        reportData.setThisWeekOrderNumber(thisWeekOrderNumber);
        reportData.setThisWeekVisitsNumber(thisWeekOrderVisitNumber);
        reportData.setThisMonthOrderNumber(thisMonthOrderNumber);
        reportData.setThisMonthVisitsNumber(thisMonthOrderVisitNumber);
    }

}
